package com.nytd.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nytd.demo.common.constant.Constant;
import com.nytd.demo.common.exception.BlogException;
import com.nytd.demo.common.util.JwtUtil;
import com.nytd.demo.common.util.SecurityUtil;
import com.nytd.demo.mapper.BlogInfoMapper;
import com.nytd.demo.mapper.UserInfoMapper;
import com.nytd.demo.pojo.dataobject.BlogInfo;
import com.nytd.demo.pojo.dataobject.UserInfo;
import com.nytd.demo.pojo.request.UserLoginRequest;
import com.nytd.demo.pojo.response.UserInfoResponse;
import com.nytd.demo.pojo.response.UserLoginResponse;
import com.nytd.demo.service.UserInfoService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.security.Security;
import java.util.HashMap;
import java.util.Map;

@Service("userInfoService")
public class UserServiceImpl implements UserInfoService {
    @Resource(name = "userInfoMapper")
    private UserInfoMapper userInfoMapper;
    @Resource(name = "blogInfoMapper")
    private BlogInfoMapper blogInfoMapper;

    @Override
    public UserLoginResponse check(UserLoginRequest request) {
        UserInfo userInfo = selectUserIngoByName(request.getUserName());
        if (userInfo == null || userInfo.getId() == null) {
            throw new BlogException(404, "用户不存在");
        }
        if (!SecurityUtil.verify(request.getPassword(), userInfo.getPassword())) {
            throw new BlogException("密码错误");
        }
        Map<String, Object> claim = new HashMap<>();
        claim.put("id", userInfo.getId());
        claim.put("userName", userInfo.getUserName());
        String jwt = JwtUtil.genJwt(claim);
        return new UserLoginResponse(userInfo.getId(), jwt);
    }

    @Override
    public UserInfoResponse getUserInfo(Integer userId) {
        UserInfo userInfo = selectUserInfoById(userId);
        if (userInfo == null) {
            throw new BlogException("用户不存在");
        }
        UserInfoResponse userInfoResponse = new UserInfoResponse();
        BeanUtils.copyProperties(userInfo, userInfoResponse);
        return userInfoResponse;
    }

    @Override
    public UserInfoResponse getAuthorInfo(Integer blogId) {
        UserInfoResponse userInfoResponse = new UserInfoResponse();
        BlogInfo blogInfo = selectBlogInfoBiId(blogId);
        if (blogInfo == null) {
            throw new BlogException("博客不存在");
        }
        UserInfo userInfo = selectUserInfoById(blogInfo.getUserId());
        BeanUtils.copyProperties(userInfo, userInfoResponse);
        return userInfoResponse;
    }

    public BlogInfo selectBlogInfoBiId(Integer blogId) {
        return blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId)
                .eq(BlogInfo::getDeleteFlag, Constant.NOT_DELETE));
    }

    public UserInfo selectUserInfoById(Integer userId) {
        return userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getId, userId)
                .eq(UserInfo::getDeleteFlag, Constant.NOT_DELETE));
    }

    public UserInfo selectUserIngoByName(String userName) {
        return userInfoMapper.selectOne(
                new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getUserName, userName)
                        .eq(UserInfo::getDeleteFlag, Constant.NOT_DELETE));
    }
}
